﻿Imports Microsoft.WindowsMobile.Samples.Location
Imports System.IO
Imports System.Reflection
Imports System.Drawing.Graphics
Imports Microsoft.WindowsCE.Forms
Imports System.Runtime.InteropServices

Public Class frmMain

    Public Const EM_SETINPUTMODE As Integer = &HDE
    Public Const EIM_SPELL As Integer = 0
    Public Const EIM_AMBIG As Integer = 1
    Public Const EIM_NUMBERS As Integer = 2
    <DllImport("coredll.dll")> _
    Public Shared Function GetFocus() As IntPtr
    End Function
    <DllImport("coredll.dll")> _
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
    ByVal Message As Integer, ByVal wParam As Integer, _
    ByVal lParam As Integer) As Integer
    End Function


    Declare Sub SystemIdleTimerReset Lib "coredll" ()
    Dim objDebug As New frmDebug
    Dim objTracking As New frmTracking
    Dim UpdateHandler As LocationChangedEventHandler
    Dim Pos As String = String.Empty
    Dim WithEvents objGps As New Gps
    Dim objLogger As clsLogger
    Dim objDistance As New clsDistanceCalc
    Dim OldLat As Double = -1
    Dim OldLon As Double = -1
    Dim SumDist As Double = 0
    Dim TrackingMode As Boolean = False
    Dim objAlert As New clsAlertManager


    Private Sub objGps_LocationChanged(ByVal sender As Object, ByVal args As Microsoft.WindowsMobile.Samples.Location.LocationChangedEventArgs) Handles objGps.LocationChanged
        Try
            If args IsNot Nothing Then
                Dim para() As Object = {sender, args}
                Me.Invoke(UpdateHandler, para)
            End If
        Catch ex As Exception
            PushLog("Error in frmMain.LocationChanged: " & ex.Message)
        End Try
    End Sub

    Private Sub Locator(ByVal sender As Object, ByVal args As LocationChangedEventArgs)
        Try
            Glob.DistanceCalc.Push(args.Position)
            Glob.FuelConsumption.Distance = Glob.DistanceCalc.Distance
            Dim objPosUtil As New clsPosition(args.Position)
            objAlert.Speed = objPosUtil.Speed
            UcGuage1.Speed = objPosUtil.Speed
            UcGuage1.SatCount = objPosUtil.SatCount
            UpdateSignal(objPosUtil.SignalStrength)
            If TrackingMode Then
                objLogger.Position = args.Position
            End If
        Catch ex As Exception
            PushLog("Error in frmMain.Locator: " & ex.Message)
        End Try
    End Sub

    Private Function GetTime() As String
        Return Now.ToShortTimeString & If(Now.Hour > 12, " PM", " AM")
    End Function

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            lblTime.Text = GetTime()
            objLogger = New clsLogger()
            UpdateHandler = New LocationChangedEventHandler(AddressOf Locator)
            UcGuage1.Width = 460
            UcGuage1.Height = 460
            UcGuage1.Location = New Point(11, 20)
        Catch ex As Exception
            PushLog("Error in frmMain.Load: " & ex.Message)
        End Try
    End Sub

    Dim IdelTimerReset As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblTime.Text = GetTime()
        IdelTimerReset += 1
        If IdelTimerReset >= 40 Then
            SystemIdleTimerReset()
            IdelTimerReset = 0
        End If
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub btnGPSStart_OnFinger() Handles btnGPSStart.OnFinger
        Try
            If Not objGps.Opened Then
                objGps.Open()
                Pos = String.Empty
                btnGPSStart.Visible = False
                btnGPSStop.Visible = True
            End If
        Catch ex As Exception
            PushLog("Error in frmMain.btnGPSStart_OnFinger: " & ex.Message)
        End Try
    End Sub

    Private Sub btnGPSStop_OnFinger() Handles btnGPSStop.OnFinger
        Try
            If objGps.Opened Then objGps.Close()
            btnGPSStart.Visible = True
            btnGPSStop.Visible = False
            Me.Close()
        Catch ex As Exception
            PushLog("Error in frmMainbtn.GPSStop_OnFinger:" & ex.Message)
        End Try
    End Sub

    Private Sub btnTrack_OnFinger() Handles btnTrack.OnFinger
        Try
            objTracking.ShowDialog()
            If objTracking.DialogResult = Windows.Forms.DialogResult.OK Then
                objLogger.KMLSettings()
                TrackingMode = True
                Glob.DistanceCalc.ResetDistance()
            End If
        Catch ex As Exception
            PushLog("Error in frmMain.btnTrack_OnFinger: " & ex.Message)
        End Try
    End Sub

    Private Sub btnExit_OnFinger() Handles btnExit.OnFinger
        Try
            If objGps.Opened Then objGps.Close()
            objLogger.Save()
            Me.Close()
        Catch ex As Exception
            PushLog("Error in frmMain.btnExit_OnFinger: " & ex.Message)
        End Try
    End Sub

    Private Sub btnSetup_OnFinger() Handles btnSetup.OnFinger
        Dim objSetup As New frmSetup
        objSetup.ShowDialog()
    End Sub

    Private Sub UpdateSignal(ByVal ST As Integer)

        If ST <= 5 Then
            picSignal.Image = My.Resources.s0
            Exit Sub
        End If

        If ST >= 6 And ST <= 15 Then
            picSignal.Image = My.Resources.s1
            Exit Sub
        End If

        If ST >= 16 And ST <= 25 Then
            picSignal.Image = My.Resources.s2
            Exit Sub
        End If

        If ST >= 26 And ST <= 35 Then
            picSignal.Image = My.Resources.s3
            Exit Sub
        End If

        If ST > 35 Then
            picSignal.Image = My.Resources.s4
            Exit Sub
        End If
    End Sub
End Class
